package com.chinafree.business.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.chinafree.common.utils.JWTUtils;
import com.chinafree.model.enumeration.JWTValue;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;

public class BaseController {


    @Autowired
    private HttpServletRequest request;

    protected Long getUserId() {
        String token = this.request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String user_id = tokenInfo.getHeaderClaim(JWTValue.USER_ID.getValue()).asString();
        return Long.valueOf(user_id);
    }

    protected Long getUserId(String token) {
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String user_id = tokenInfo.getHeaderClaim(JWTValue.USER_ID.getValue()).asString();
        return Long.valueOf(user_id);
    }
    

    protected Long getCompanyId() {
        String token = this.request.getHeader("token");
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String user_id = tokenInfo.getHeaderClaim(JWTValue.COMPANY_ID.getValue()).asString();
        return Long.valueOf(user_id);
    }
    protected Long getCompanyId(String token) {
        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
        String user_id = tokenInfo.getHeaderClaim(JWTValue.COMPANY_ID.getValue()).asString();
        return Long.valueOf(user_id);
    }
}

